/*Outreach splicing game*/

// Constants - their value won't change.
// They're used here to set pin numbers:
const int buttonPin = 0;     // the number of the pushbutton pin

// Variables - their values will change:
int gameCounter = 0;           // variable for checking how many times the button has been pressed
int gSpeed = 800;       // initial game speed - time in milliseconds each led is turned on
int prevButtonState = LOW;    // Variable to ensure two button presses are NOT recorded if the button is simply held down
int currentButtonState = LOW; // Variable to ensure two button presses are NOT recorded if the button is simply held down
int firstPress = 0;       // Variable to record which LED was active when the button was pressed the first time
int secondPress = 0;      // Variable to record which LED was active when the button was pressed the second time
bool buttonPressed = false;   // Variable to tell you when to end for loops

// the setup function runs once when you press reset or power the board
void setup() {
  // Initialize all digital pins as output pins.
  for(int i = 1; i<14; i++){
    pinMode(i, OUTPUT);
  }
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  
}

//used in loop for logic
void levelUp(int press1, int press2){
      if (press1 == 4 && press2 == 9){
        //Level PASSED!! Level up!
        //update gSpeed value
        gSpeed = gSpeed/2;


      }
      else{
        //FAIL
        //digitalWrite(13, HIGH);
        //delay(4000); //10 second delay
        //digitalWrite(13, LOW);
      }
      //Flash winning values
        for(int j = 0; j<6; j++){
          digitalWrite(press1, HIGH);
          delay(500); //0.5 seconds on
          digitalWrite(press1, LOW);
          digitalWrite(press2, HIGH);
          delay(500); //0.5 seconds on 
          digitalWrite(press2, LOW);  }
}

//handle what happens when button is pressed
bool gameLogic(int i){

    bool output = false;
    
    //get current push button state
    currentButtonState = ! digitalRead(buttonPin);

    if(currentButtonState == HIGH){//button is pressed
      //check to see if button is just being held down
      if(prevButtonState == HIGH){
        //pass. Button is being held down. 'Previous button state' remains high
        }
      else{
        
        //New button press
        //increment game counter
        gameCounter ++;

        //test to see if time to end game
        if(gameCounter == 2){
          //end game
          //set game counter to 0
          gameCounter = 0;
          //set second press position identifier
          secondPress = i;

          //turn off most recent led
          digitalWrite(i, LOW);
          
          //display result and continue/level up
          if(firstPress<secondPress) levelUp(firstPress, secondPress);
          else levelUp(secondPress, firstPress);

          //reset first and second press values
          firstPress = 0;
          secondPress = 0;

          //set previous button press to LOW
          prevButtonState = LOW;
          currentButtonState = LOW;
          
          //break out of the for loop to restart the game for the next round
          output = true;
      
          //Turn off current LED
          digitalWrite(i, LOW);
        }
        else{
      //Button HAS been pressed, but this is the first time this game it has been pressed, so continue the game.
          // first time button pressed. set first press position identifier
          firstPress = i;
          
          //set previous button state to HIGH
          prevButtonState = HIGH;
        }
        
      }
    }
    else{//button not pressed. Set previous button press val to LOW
      prevButtonState = LOW;
    }

    return output;
}


// the loop function runs over and over again forever
void loop() {
  
/*=============FIRST=FOR=LOOP=======Increasing-pin-value======================================*/
  for(int i = 2; i<14; i++){
    /* turn the LED on (HIGH is the voltage level) */
    digitalWrite(i, HIGH);  
  
    // wait for designated time
  /*
  The reason we use a for loop here is so the program will check the button every millisecond.
  Otherwise, the arduino will just sit there and not do anything - 
  it will not detect any new input, it won't make anything else happen until it has finished
  executing the delay command. In every loop of the for loop, the arduino will wait ("delay")
  for one millisecond, check to see if the button has been pressed, and then start the loop again
  if the button has NOT been pressed.
  */
    for(int k = 0; k<gSpeed; k++){
    //wait one millisecond
      delay(1);
    
      //perform game logic (check to see if the button is pressed)
      buttonPressed = gameLogic(i);
    
    //if the button has been pressed, exit the for loop.
      if (buttonPressed) break;
    }
  
  //if the button has been pressed, exit the second for loop
    if(buttonPressed){
      buttonPressed = false;
      break;  
    }
    
    digitalWrite(i, LOW);    // turn the LED off by making the voltage LOW
  }

  
/*=============SECOND=FOR=LOOP=======Decreasing-pin-value========================================*/
/*=============SAME=CODE=AS=ABOVE=EXCEPT=THE=FIRST=FOR=LOOP======================================*/
  for(int i = 12; i>0; i--){
    /* turn the LED on (HIGH is the voltage level) */
    digitalWrite(i, HIGH); 

      // wait for designated time              
      for(int k = 0; k<gSpeed; k++){
        delay(1);
        //perform game logic
        buttonPressed = gameLogic(i);
        if (buttonPressed) break;
      }
      if(buttonPressed){
        buttonPressed = false;
        break;  
      }
    
      digitalWrite(i, LOW);    // turn the LED off by making the voltage LOW
  }
  
}

/*===EXTRA=CODE================================================================================*/

/*===MORE=ADVANCED=GAME=====VARIABLE=SPEED=LEDS================================================*/

    /*if (i == 4 || i == 9) delay(gSpeed/3);
    else if (i == 3 || i == 5 || i == 8 || i == 10) delay(gSpeed/2);
    else delay(gSpeed);  */  

/*===SERIAL===================================================================================*/  
  
  //If we need to use the serial
  //Begin serial
  //Serial.begin(9600);
  //Serial.println("this will be printed to the serial"))